/* 判断各个浏览器版本号 */
import {resultCustom} from "@/utils/rest/notifyHint";

function matchVesion() {
    let userAgent = navigator.userAgent;
    let userAgentLowerCase = userAgent.toLowerCase();
    // IE浏览器
    let matchIE = /(msie\s|trident.*rv:)([\w.]+)/.exec(userAgentLowerCase);
    if (matchIE !== null) {
        return "IE " + matchIE[1];
    }
    // Edge浏览器
    let matchEdge = /(edge)\/([\w.]+)/.exec(userAgentLowerCase);
    if (matchEdge !== null) {
        return "Edge " + matchEdge[2];
    }
    // Firefox浏览器
    let matchFirefox = /(firefox)\/([\w.]+)/.exec(userAgentLowerCase);
    if (matchFirefox !== null) {
        return "Firefox " + matchFirefox[2];
    }
    // Opera浏览器
    let matchOpera = /(opera).+version\/([\w.]+)/.exec(userAgentLowerCase);
    if (matchOpera !== null) {
        return "Opera " + matchOpera[2];
    }
    // Chrome浏览器
    let matchChrome = /(chrome)\/([\w.]+)/.exec(userAgentLowerCase);
    if (matchChrome !== null) {
        return "Chrome " + matchChrome[2];
    }
    // Safari浏览器
    let matchSafari = /version\/([\w.]+).*(safari)/.exec(userAgentLowerCase);
    if (matchSafari !== null) {
        return "Safari " + matchSafari[1];
    }
    // 如果没有匹配到任何浏览器，返回未知
    return "未知浏览器 0";
}

export function getOperationSystem() {
    let userAgent = navigator.userAgent;
    // 检测Android并提取版本
    if (/Android (\d+(?:\.\d+)*);/.test(userAgent)) {
        const androidMatch = userAgent.match(/Android (\d+(?:\.\d+)*);/);
        if (androidMatch) {
            return "Android " + androidMatch[1];
        }
    }
    // 检测iOS设备
    if (/iPad|iPhone|iPod/.test(userAgent)) {
        const versionMatch = userAgent.match(/OS (\d+[_-]\d+[_-]\d+)/);
        if (versionMatch) {
            const version = versionMatch[1].replace(/_/g, ".");
            return "iOS " + version;
        }
    }
    // 检测MacOS
    if (userAgent.indexOf("Macintosh") !== -1) {
        return "MacOS";
    }
    // 检测Windows操作系统
    if (/Windows NT (\d+\.\d+)/.test(userAgent)) {
        let match = userAgent.match(/Windows NT (\d+\.\d+)/);
        if (match) {
            return "Windows " + match[1];
        }
    }
    // 如果不是以上任何系统，那么可能是Linux或其他未知系统
    if (userAgent.indexOf("Linux") !== -1) {
        return "Linux";
    }
    return "Unknown";
}

export  function initBrowserVersion() {
    let browserMatch = matchVesion();
    return browserMatch;
}

/**
 * 复制事件
 * @param copyContent
 */
export async function copyEvent(copyContent: string, dialog: any) {
    if (navigator.clipboard && window.isSecureContext) {
        await navigator.clipboard.writeText(copyContent).then( () => {
            resultCustom(dialog, "复制成功" )
        }).catch(() => {console.log("复制失败")})
    } else {
        let textarea = document.createElement('textarea')
        textarea.value = copyContent;
        document.body.appendChild(textarea);
        textarea.select();
        const success = document.execCommand('copy');
        if (success) {
            resultCustom(dialog, "复制成功" )
        }
        document.body.removeChild(textarea);
    }
}
